<?php
namespace Rootadmin\Controller;
use Think\Controller;
class ShopController extends AdminController {
    //商户管理
    public function shop(){
        $where="1";
        $linker=I("linker","");
        if($linker!=""){
            $where.=" and linker like '%$linker%'";
            $this->assign("linker",$linker);
            $wherepage['linker']=$linker;
        }
        if($_POST["starttime"] || $_POST["endtime"]){
            $starttime=strtotime($_POST["starttime"]);
            $endtime=strtotime($_POST["endtime"]);
            $endtime=strtotime(date("Y-m-d 23:59:59",$endtime));
            if(($endtime-$starttime)<0){
                $this->error("结束日期必须大于开始日期",U("Rootadmin/shop/shop"));
            }
            $where.=" and add_time >= $starttime and add_time <= $endtime";
            $this->assign("starttime",$_POST["starttime"]);
            $this->assign("endtime",$_POST["endtime"]);
        }
        $count=M("shop")->where($where)->count();
        $Page=new \Think\Page($count,10);
        foreach($wherepage as $key=>$val) {
            $Page->parameter[$key]   =   $val;
        }
        $info=M("shop")->where($where)->limit($Page->firstRow.','.$Page->listRows)->order("id desc")->select();

        $this->assign("pages",$Page->show());
        $this->assign("info",$info);
        $this->display();
    }

    //添加商户
    public function add(){
        if($_POST){
            $data["linker"]=I("linker","");
            $data["phone"]=I("phone","");
            $data["account"]=I("account","");
            $data["type"]=I("type","");
            $data["rate"]=I("rate","");
            $data["state"]=0;
            $data["money"]=0;
            $expiry_time=I("expiry_time","");
            if($expiry_time=="1"){
                $data["expiry_time"]=strtotime('+1 month') ;
            }elseif ($expiry_time=="2"){
                $data["expiry_time"]=strtotime('+6 month') ;
            }elseif ($expiry_time=="3"){
                $data["expiry_time"]=strtotime('+12 month') ;
            }elseif ($expiry_time==""){
                $data["expiry_time"]="" ;
            }
            $data["password"]=I("password","","md5");
            $data["add_time"]=time();
            if(M("shop")->add($data)){
                $this->success("商户添加成功",U('Rootadmin/shop/shop'));
            }else{
                $this->error("商户添加失败",U('Rootadmin/shop/shop'));
            }
        }
        else{
            $this->display();
        }
    }
    //修改商户信息
    public function edit(){
        $id=I("id","");
        if($_POST){
            $data["linker"]=I("linker","");
            $data["phone"]=I("phone","");
            $data["account"]=I("account","");
            $data["type"]=I("type","");
            $data["rate"]=I("rate","");
            $expiry_time=I("expiry_time","");
            if($expiry_time=="1"){
                $data["expiry_time"]=strtotime('+1 month') ;
            }elseif ($expiry_time=="2"){
                $data["expiry_time"]=strtotime('+6 month') ;
            }elseif ($expiry_time=="3"){
                $data["expiry_time"]=strtotime('+12 month') ;
            }elseif ($expiry_time==""){
                $data["expiry_time"]="" ;
            }
            if(I("password","")!=""){
                $data["password"]=I("password","","md5");
            }
            $res=M("shop")->where("id=$id")->save($data);
            if($res===false){
                $this->error("商户修改失败",U('Rootadmin/shop/shop'));
            }else{
                $this->success("商户修改成功",U('Rootadmin/shop/shop'));
            }
        }
        else{
            $info=M("shop")->where("id=$id")->find();
            $this->assign("info",$info);
            $this->display();
        }
    }
    //删除商户
    public function delete(){
        $id=I("id","");
        if(M("shop")->where("id=$id")->delete()){
            $this->ajaxReturn(array("code"=>200,"msg"=>"商户删除成功"));
        }else{
            $this->ajaxReturn(array("code"=>201,"msg"=>"商户删除失败"));
        }
    }
    //修改商户的启用、停用状态
    public function changeUp(){
        $id=$_GET["id"];
        $up = M("shop")->where("id=$id")->getField("state");
        $up = $up==1?0:1;
        M("shop")->where("id=$id")->setField("state", $up);
        $this->ajaxReturn(array(
            'up' => $up
        ));
    }
    //商户生成的二维码
    public function qrcode(){
        $count=M("qrcode")->count();
        $Page=new \Think\Page($count,10);
        $info=M("qrcode")->limit($Page->firstRow.','.$Page->listRows)->order("id desc")->select();
        foreach ($info as $key=>$value){
            $shop=M("shop")->where("id=".$value["shop_id"])->find();
            $info[$key]["linker"]=$shop["linker"];
            $info[$key]["phone"]=$shop["phone"];
        }
        $this->assign("pages",$Page->show());
        $this->assign("info",$info);
        $this->display();
    }
    //审核商户生成的二维码
    public function pass(){
        $id=$_GET["id"];
        $up = M("qrcode")->where("id=$id")->getField("status");
        $up = $up==1?0:1;
        M("qrcode")->where("id=$id")->setField("status", $up);
        $this->ajaxReturn(array(
            'up' => $up
        ));
    }


}